package net.tycmc.cty.ctyms.timejob.iems.service;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.dao.IBaseDAO;
import net.tycmc.bulb.common.dao.ITransactionDAO;
import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.bulb.dbutil.global.GlobalDBVar;
import net.tycmc.cty.ctyms.timejob.common.DynamicReadConfig;
import net.tycmc.cty.ctyms.timejob.common.EquipVersionCommon;
import net.tycmc.cty.ctyms.timejob.common.MyConnectDSDAO;
import net.tycmc.cty.ctyms.timejob.common.MyConnectDSTransDAO;
import net.tycmc.cty.ctyms.timejob.iems.soa.client.vclbase.VclBaseHandler;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.cxf.binding.corba.wsdl.Array;

/**
 * 自动安装
 * @author liuhuanhuan
 * 更新时间：2012-11-24
 * 作者：程世朝
 */
public class AutoInstallVclService {
	private MyConnectDSDAO mcdsDAO = new MyConnectDSDAO();
	private MyConnectDSTransDAO mcdstDAO = new MyConnectDSTransDAO();
	
	//设备型号ID默认为0，即不属于任何型号
	private final static String vclTypeId = "0";
	
	//设备类型ID默认为0，即不属于任何类型
	private final static String vclKindId = "0";
	
	//省份ID默认为0，即不属于省份
	private final static String provinceId = "0";
	
	//UDS默认频率（单位：分钟）
	private final static int FREQUDSDEF = 10;
	
	//PGN默认频率（单位：分钟）
	private final static int FREQPGNDEF = 10;
	
	private IBaseDAO baseDAO;
	
	private ITransactionDAO tranDAO;
	
	private VclBaseHandler vclBaseHandler;

	public IBaseDAO getBaseDAO() {
		return baseDAO;
	}

	public void setBaseDAO(IBaseDAO baseDAO) {
		this.baseDAO = baseDAO;
	}

	public ITransactionDAO getTranDAO() {
		return tranDAO;
	}

	public void setTranDAO(ITransactionDAO tranDAO) {
		this.tranDAO = tranDAO;
	}

	public VclBaseHandler getVclBaseHandler() {
		return vclBaseHandler;
	}

	public void setVclBaseHandler(VclBaseHandler vclBaseHandler) {
		this.vclBaseHandler = vclBaseHandler;
	}
	
	
	/**
	 * 终端自动安装，逻辑重新整理完善--fujichaun
	 * 
	 */
	@SuppressWarnings("unchecked")
	public void autoInstallVclService() {
		System.out.println("自动安装作业开始：["+DateUtil.toString(DateUtil.now(), "yyyy-MM-dd HH:mm:ss")+"~");
		//当前月
		String date = DateUtil.toString(DateUtil.now(), "yyyyMM");
		System.out.println("自动安装作业开始--当月:"+ date);
		//上一月
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.MONTH, -1);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		String lastDate = new SimpleDateFormat("yyyyMM").format(cal.getTime());
		System.out.println("自动安装作业开始--上月:"+ lastDate);
		String accOr = "定时扫描任务";
		String accOrInstallRem = "终端自动安装";
		String accOrOutRem = "终端自动拆除";
		
		//系统ID
		String DataUnity_SysIemsID = "";
		String DataUnity_SysCmmsID = "";
		String DataUnity_SysCesID = "";
		//数据库名
		String BaseNameIems = "";
//		String BaseNameCmms = "";
		String BaseNameCes = "";
		String softVers = "";
		try {
			softVers = EquipVersionCommon.getSoftVerByAgrType("AutoTmnlInstall");
			DataUnity_SysIemsID = DynamicReadConfig.getConfigProperty("config.properties", "DataUnity_SysImesID");//系统ID IEMS
			DataUnity_SysCmmsID = DynamicReadConfig.getConfigProperty("config.properties", "DataUnity_SysCmmsID");//系统ID CMMS
			DataUnity_SysCesID = DynamicReadConfig.getConfigProperty("config.properties", "DataUnity_SysCesID");//系统ID CES
			BaseNameIems = DynamicReadConfig.getConfigProperty("config.properties", "BaseNameIems");//数据库名 IEMS
//			BaseNameCmms = DynamicReadConfig.getConfigProperty("config.properties", "BaseNameCmms");// 数据库名 CMMS
			BaseNameCes = DynamicReadConfig.getConfigProperty("config.properties", "BaseNameCes");//数据库名 CES
		} catch (ConfigurationException e1) {
			e1.printStackTrace();
		}
		
		//获取服务器IP
		String localIp = "";
		try {
			localIp = InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}
		
		//判断当前终端是否存在
//		String sqlTmnl = "SELECT * FROM "+GlobalDBVar.getDBFullName_Conn("Tmnl_Info", "1", null)+" WHERE Tmnl_ID = ? AND Tmnl_SysI_ID in ("+DataUnity_SysIemsID+","+DataUnity_SysCmmsID+","+DataUnity_SysCesID+") AND GETDATE() between Tmnl_Eff and Tmnl_Exp";
		String sqlTmnl = "SELECT * FROM "+GlobalDBVar.getDBFullName_Conn("Tmnl_Info", "1", null)+" WHERE Tmnl_ID = ? AND Tmnl_SysI_ID in ("+DataUnity_SysIemsID+","+DataUnity_SysCmmsID+","+DataUnity_SysCesID+") AND GETDATE() between Tmnl_Eff and Tmnl_Exp";
	
		//新一批终端设备设置的默认参数
		String tmnlParam = "select TmnlIL_Tmnl_ID,TmnlIL_vcl_id,Tmnl_TmnlUPB_ID,TmnlIL_InstallTime from "+GlobalDBVar.getDBFullName_Conn("tmnl_install_last", "1", null)+" " +
				" inner join  "+GlobalDBVar.getDBFullName_Conn("tmnl_info", "1", null)+" on TmnlIL_Tmnl_ID = Tmnl_ID " +
				" inner join  "+GlobalDBVar.getDBFullName_Conn("Tmnl_Info_UdsPgnBatch_Param", "1", null)+" on TmnlUPBP_TmnlUPB_ID = Tmnl_TmnlUPB_ID " +
				" where TmnlIL_vcl_id = ? ";

		//设备安装记录获取-根据设备
		String sqlInstallKeyVcl = "SELECT * FROM "+GlobalDBVar.getDBFullName_Conn("Tmnl_Install_Last", "1", null)+" WHERE TmnlIL_Vcl_ID = ?";
		//设备安装记录获取-根据终端
		String sqlInstallKeyTmnl = "SELECT * FROM "+GlobalDBVar.getDBFullName_Conn("Tmnl_Install_Last", "1", null)+" WHERE TmnlIL_Tmnl_ID = ?";
		//设备是否在本系统中存在
		String sqlVclExist = "select COUNT(1) cnt from "+GlobalDBVar.getDBFullName_Conn("Vcl_Info", "1", null)+" where Vcl_No=? and GETDATE() between Vcl_Eff and Vcl_Exp";
		//获取车辆信息跟踪记录
		//先查询自动安装标记类型是否存在
		String sqlIS = "";
		String strIsExit = "select TOP 1 * from Msg_LoadAwokeSign where MsgLAS_StatType=2 and MsgLAS_SignDate is not null order by msglas_id desc ";
		Map<String,Object> preIsExit = baseDAO.execQueryOne(strIsExit, new Object[]{});
		//上月标记
		Map<String,Object> LastMonthSign = new HashMap<String, Object>();
		//当前月标记
		Map<String,Object> MonthSign = new HashMap<String, Object>();
		if(preIsExit == null || preIsExit.isEmpty()){
			//不存在，初始化标记(当前月份)
			sqlIS = "SELECT Top 1 * FROM "+GlobalDBVar.getDBFullName_Conn("Msg_Track_"+date, "3", null)+" order by MsgTk_ID desc";
			Map<String,Object> initSignMap = baseDAO.execQueryOne(sqlIS, new Object[]{});
			String initSignSQL = "insert into Msg_LoadAwokeSign(MsgLAS_MsgT_ID,MsgLAS_PreStatTime,MsgLAS_StatType,MsgLAS_SignDate)values(?,?,2,?)";
			mcdsDAO.execInsert(initSignSQL, new Object[]{MapGetter.getString(initSignMap, "MsgTk_ID"),MapGetter.getString(initSignMap, "MsgTk_MsgTime"),date});
			return ;
		}else {
			//查询上月标记
			String IsLastMonthSign = "select * from Msg_LoadAwokeSign where MsgLAS_StatType=2 and MsgLAS_SignDate = ? order by msglas_id desc ";
			LastMonthSign = baseDAO.execQueryOne(IsLastMonthSign, new Object[]{lastDate});
			//查询当月标记
			String IsMonthSign = "select * from Msg_LoadAwokeSign where MsgLAS_StatType=2 and MsgLAS_SignDate = ? order by msglas_id desc ";
			MonthSign = baseDAO.execQueryOne(IsMonthSign, new Object[]{date});
			
		}
		String sql = "";
		//更新标记为当前月份标记
		sql = "SELECT Top 1 * FROM "+GlobalDBVar.getDBFullName_Conn("Msg_Track_"+date, "3", null)+" order by MsgTk_ID desc";
		Map<String,Object> updateSignMap = baseDAO.execQueryOne(sql, new Object[]{});		
		String updateSignSQL = "update Msg_LoadAwokeSign set MsgLAS_MsgT_ID=?,MsgLAS_PreStatTime=?,MsgLAS_SignDate = '"+date+"' where MsgLAS_StatType=2  ";
		mcdsDAO.execUpdate(updateSignSQL, new Object[]{MapGetter.getString(updateSignMap, "MsgTk_ID"),MapGetter.getString(updateSignMap, "MsgTk_MsgTime")});
	
		//获取信息跟踪记录
		sql = "SELECT * FROM "+GlobalDBVar.getDBFullName_Conn("Msg_Track_"+date, "3", null)+" where MsgTk_ID>? and MsgTk_ID<=? ";
		//上月标记存在
		if(LastMonthSign!=null && LastMonthSign.size()>0){
			sql+=" union all SELECT * FROM "+GlobalDBVar.getDBFullName_Conn("Msg_Track_"+lastDate, "3", null)+" where MsgTk_ID>? ";
		}
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		//上月标记存在
		if(LastMonthSign!=null && LastMonthSign.size()>0){
			 list = baseDAO.execQuery(sql, new Object[]{MapGetter.getString(MonthSign, "MsgLAS_MsgT_ID"),MapGetter.getString(updateSignMap, "MsgTk_ID"),MapGetter.getString(LastMonthSign, "MsgLAS_MsgT_ID")});		
		} else {
			 list = baseDAO.execQuery(sql, new Object[]{MapGetter.getString(MonthSign, "MsgLAS_MsgT_ID"),MapGetter.getString(updateSignMap, "MsgTk_ID")});
		}
		
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {//循环车辆信息跟踪信息
			Map<String, Object> map = (Map<String, Object>) iterator.next();
			String msgTk_ID = MapGetter.getString(map, "MsgTk_ID");
			String tmnlId = MapGetter.getString(map, "MsgTk_Tmnl_ID");//返回的终端id
			String vclNo = MapGetter.getString(map, "MsgTk_ProductSerialNo");//返回的发动机号
			//判断返回的发动机号是否为有效发动机号（8位纯数字为有效）
			//1.为空-不满足，直接返回
			if(!StringUtil.isNullOrEmpty(vclNo.trim()))continue;
			//发动机号为8位而且是纯数字
			boolean isf = vclNo.trim().matches("[0-9]+");
			if(vclNo.trim().length() == 8 && isf){
			//查询车辆信息跟踪中的终端是否存在
			Map<String,Object> tmnlMap = baseDAO.execQueryOne(sqlTmnl, new Object[]{tmnlId});
			if(!tmnlMap.isEmpty() 
					&& softVers.indexOf(MapGetter.getString(tmnlMap, "Tmnl_SoftEdition").toUpperCase())>=0){//终端存在，且软件版本号支持终端自动安装功能
				
				String dataUnity_SysID=MapGetter.getString(tmnlMap, "Tmnl_SysI_ID");//系统ID ，终端所属系统
				//CMMS系统终端，目前默认按IEMS处理  TODO
				//设备是否为本系统创建
				String sqlVclCreateSys = "select * from "+GlobalDBVar.getDBFullName_Conn("Vcl_Info", "1", null)+" where Vcl_SysI_ID="+dataUnity_SysID+" and Vcl_ID=? ";
				//通过发动机编号获取设备号
				String sqlVclNum = "";
				//获取被拆除的设备信息及终端ID
				String sqlVclInfo = "";
				if(DataUnity_SysCmmsID.equals(dataUnity_SysID)){
						dataUnity_SysID = "2";
					}
				if(DataUnity_SysIemsID.equals(dataUnity_SysID)){
					 sqlVclNum = "select vcl_id as VehicleID,vcl_no as VehicleNumber,* from "+GlobalDBVar.getDBFullName_Conn("vcl_info", "1", null)+" " +
					 		" inner join  "+GlobalDBVar.getDBFullName_Conn("System_Vehicle", "1", null)+" ON Vcl_ID = SV_Vcl_ID AND SV_SysI_ID in ("+DataUnity_SysIemsID+","+DataUnity_SysCmmsID+") AND system_vehicle.SV_QueryPurv = 1 " +
					 		" inner join  "+BaseNameIems+"vehicleinfo on vcl_id = vehicleid  where ESNNumber='"+vclNo+"' ";
					 
					 sqlVclInfo = "select TmnlI_Vcl_ID,VehicleNumber,TmnlI_Tmnl_ID,ESNNumber from "+GlobalDBVar.getDBFullName_Conn("Tmnl_Install", "1", null)+" "
								+" inner join  "+GlobalDBVar.getDBFullName_Conn("System_Vehicle", "1", null)+" ON TmnlI_Vcl_ID = SV_Vcl_ID AND SV_SysI_ID in ("+DataUnity_SysIemsID+","+DataUnity_SysCmmsID+") AND system_vehicle.SV_QueryPurv = 1 " 
								+" left join "+BaseNameIems+"vehicleinfo on VehicleID=TmnlI_Vcl_ID where TmnlI_ID=?";
				}
//				if(DataUnity_SysCmmsID.equals(dataUnity_SysID)){
//					sqlVclNum = "select vcl_id as VehicleID,vcl_no as VehicleNumber,* from "+GlobalDBVar.getDBFullName_Conn("vcl_info", "1", null)+" " +
//							" inner join  "+GlobalDBVar.getDBFullName_Conn("System_Vehicle", "1", null)+" ON Vcl_ID = SV_Vcl_ID AND SV_SysI_ID = "+DataUnity_SysCmmsID+" AND system_vehicle.SV_QueryPurv = 1 " +
//							" inner join  "+BaseNameCmms+"vehicleinfo on vcl_id = vehicleid where ESNNumber='"+vclNo+"' ";
//					
//					 sqlVclInfo = "select TmnlI_Vcl_ID,VehicleNumber,TmnlI_Tmnl_ID,ESNNumber from "+GlobalDBVar.getDBFullName_Conn("Tmnl_Install", "1", null)+" "
//								+" inner join  "+GlobalDBVar.getDBFullName_Conn("System_Vehicle", "1", null)+" ON TmnlI_Vcl_ID = SV_Vcl_ID AND SV_SysI_ID = "+DataUnity_SysCmmsID+" AND system_vehicle.SV_QueryPurv = 1 " 
//								+" inner join "+BaseNameCmms+"vehicleinfo on VehicleID=TmnlI_Vcl_ID where TmnlI_ID=?";
//				}
				if(DataUnity_SysCesID.equals(dataUnity_SysID)){
					sqlVclNum = "select T.vcl_id as VehicleID,T.vcl_no as VehicleNumber,* from "+GlobalDBVar.getDBFullName_Conn("vcl_info", "1", null)+" T " +
							" inner join  "+GlobalDBVar.getDBFullName_Conn("System_Vehicle", "1", null)+" ON Vcl_ID = SV_Vcl_ID AND SV_SysI_ID = "+DataUnity_SysCesID+" AND system_vehicle.SV_QueryPurv = 1 " +
							" inner join  "+BaseNameCes+"Vcl_Info CT on T.vcl_id = CT.vcl_id where Vcl_EgnNo='"+vclNo+"' ";
					
					 sqlVclInfo = "select TmnlI_Vcl_ID,VCL_NO AS VehicleNumber,TmnlI_Tmnl_ID,Vcl_EgnNo AS ESNNumber from "+GlobalDBVar.getDBFullName_Conn("Tmnl_Install", "1", null)+" "
								+" inner join  "+GlobalDBVar.getDBFullName_Conn("System_Vehicle", "1", null)+" ON TmnlI_Vcl_ID = SV_Vcl_ID AND SV_SysI_ID = "+DataUnity_SysCesID+" AND system_vehicle.SV_QueryPurv = 1 " 
								+" inner join "+BaseNameCes+"Vcl_Info on vcl_id=TmnlI_Vcl_ID where TmnlI_ID=?";
				}
				
				Map<String,Object> mapVcl = baseDAO.execQueryOne(sqlVclNum, new Object[]{});
				if(!mapVcl.isEmpty()){//设备存在
					
					//设备ID
					String vclId = MapGetter.getString(mapVcl, "VehicleID");
					//跟踪信息返回的设备号
					String vehicleNumber = MapGetter.getString(mapVcl, "VehicleNumber");
					//验证设备是否安装了终端
					Map<String,Object> mapInstallKeyVal = baseDAO.execQueryOne(sqlInstallKeyVcl, new Object[]{vclId});
					if(!mapInstallKeyVal.isEmpty()){//安装了终端
						//安装的终端ID
						String installTmnlId = MapGetter.getString(mapInstallKeyVal, "TmnlIL_Tmnl_ID");
						//终端安装记录ID
						String tmnlInstallId = MapGetter.getString(mapInstallKeyVal, "TmnlIL_TmnlI_ID");
						
						if(installTmnlId.equals(tmnlId)){//安装终端与车辆信息跟踪中的终端ID一致，无须进行以下操作
							continue;
						}else {
							//不一致，进行拆除提醒
							//跟踪信息返回的设备号
							String msgVehicleNumber = vehicleNumber;
							//系统配置的终端号
							String systemTmnlId = installTmnlId;
							//采集数据的终端号-跟踪信息返回的终端号
							String msgTmnlId = tmnlId;
							//提醒信息
							String MsgP_DetailMsg = msgVehicleNumber+"设备,系统配置的终端号为："+systemTmnlId+","+"采集数据的终端号为："+msgTmnlId;
							//jiyongtian 20150215 iems3.7优化，修改为入库设备自动安装提示表
							String sql_Prompt = "";
							if(DataUnity_SysCesID.equals(dataUnity_SysID)){
								System.out.println("跟踪信息ID："+msgTk_ID+",属于CES系统，不用插入提示！提示信息："+MsgP_DetailMsg);
							} else {
								if(DataUnity_SysIemsID.equals(dataUnity_SysID)){
									sql_Prompt = "insert into  "+BaseNameIems+"Vcl_AutoInstallPrompt(VAIP_VclID,VAIP_Type,VAIP_SaveTime,VAIP_MsgContent)values(?,2,GETDATE(),?)";
								}
//								if(DataUnity_SysCmmsID.equals(dataUnity_SysID)){
//									sql_Prompt = "insert into  "+BaseNameCmms+"Vcl_AutoInstallPrompt(VAIP_VclID,VAIP_Type,VAIP_SaveTime,VAIP_MsgContent)values(?,2,GETDATE(),?)";
//								}
								/*String sql_Prompt = "insert into Msg_Prompt(MsgP_VclID,MsgP_MsgTime,MsgP_Type,MsgP_State,MsgP_DBName,MsgP_TableName,MsgP_UpdateTime,MsgP_CreateTime,MsgP_DetailMsg) "
										+"values(?,?,8,8,'M','Prompt',GETDATE(),GETDATE(),?)";*/
								int num = mcdsDAO.execInsert(sql_Prompt, new Object[]{vclId,MsgP_DetailMsg});
								if(num>0){
									System.out.println("跟踪信息ID："+msgTk_ID+",插入到提示表-----成功！");
								}else{
									System.out.println("跟踪信息ID："+msgTk_ID+",插入到提示表-----失败！");
								}
							}
							
						}
					}else{//未安装终端
						
						//车辆跟踪信息中的终端是否被安装
						Map<String,Object> mapInstallKeyTmnl = baseDAO.execQueryOne(sqlInstallKeyTmnl, new Object[]{tmnlId});
						if(!mapInstallKeyTmnl.isEmpty()){//被安装，进行拆提醒
							//终端安装记录ID
							String tmnlInstallId = MapGetter.getString(mapInstallKeyTmnl, "TmnlIL_TmnlI_ID");
							Map<String,Object> params = new HashMap<String,Object>();
							params.put("TmnlI_ID", tmnlInstallId);
							params.put("SysI_ID_B", dataUnity_SysID);
							params.put("BackoutPerson", "");
							params.put("BackoutPlace", "");
							params.put("BackoutCause", "");
							params.put("Remark", accOrOutRem);
							params.put("OprtAccount", accOr);
							params.put("OprtTime", DateUtil.toString(DateUtil.now()));
							params.put("OprtIP", localIp);
							params.put("OprtSysI_ID", dataUnity_SysID);
							if(StringUtil.isValid(tmnlInstallId)){
								
							Map<String,Object> vclMap = baseDAO.execQueryOne(sqlVclInfo, new Object[]{tmnlInstallId});
							//与返回终端已经安装的设备
							String VehicleNumber = MapGetter.getString(vclMap, "VehicleNumber");
							//跟踪信息返回的设备号
							String msgVehicleNumber = vehicleNumber;
							//采集数据的终端号-跟踪信息返回的终端号
							String msgTmnlId = tmnlId;
							//提醒信息
							String MsgP_DetailMsg = msgVehicleNumber+"设备,与终端"+msgTmnlId+"自动安装失败,"+"原因是终端"+msgTmnlId+"与设备"+VehicleNumber+"已绑定";
							//jiyongtian 20150215 iems3.7优化，修改为入库设备自动安装提示表
							if(DataUnity_SysCesID.equals(dataUnity_SysID)){
								System.out.println("跟踪信息ID："+msgTk_ID+",属于CES系统，不用插入提示！提示信息："+MsgP_DetailMsg);
							} else {
								String sql_Prompt = "";
								if(DataUnity_SysIemsID.equals(dataUnity_SysID)){
									sql_Prompt = "insert into  "+BaseNameIems+"Vcl_AutoInstallPrompt(VAIP_VclID,VAIP_Type,VAIP_SaveTime,VAIP_MsgContent)values(?,2,GETDATE(),?)";
								}
//								if(DataUnity_SysCmmsID.equals(dataUnity_SysID)){
//									sql_Prompt = "insert into  "+BaseNameCmms+"Vcl_AutoInstallPrompt(VAIP_VclID,VAIP_Type,VAIP_SaveTime,VAIP_MsgContent)values(?,2,GETDATE(),?)";
//								}
								int num = mcdsDAO.execInsert(sql_Prompt, new Object[]{vclId,MsgP_DetailMsg});
								if(num>0){
										System.out.println("跟踪信息ID："+msgTk_ID+",插入到提示表-----成功！");
								}else{
										System.out.println("跟踪信息ID："+msgTk_ID+",插入到提示表-----失败！");
								}
							}
						}
					} else {
						//没有被安装，进行自动安装
						//安装前先判断此设备是否为本系统所创建
						//设备是否为本系统创建
						Map<String,Object> vclCreateSysMap = baseDAO.execQueryOne(sqlVclCreateSys, new Object[]{vclId});
						if(vclCreateSysMap.isEmpty()){//非本系统创建
							continue;
						}else {
							//是本系统创建，进行自动安装
							if(StringUtil.isNullOrEmpty(vclId)&&StringUtil.isNullOrEmpty(tmnlId)){
								//执行全新的安装
								Map<String,Object> params = new HashMap<String,Object>();
								params.put("Vcl_ID", vclId);
								params.put("Tmnl_ID", tmnlId);
								params.put("SysI_ID_I", dataUnity_SysID);
								params.put("InstallPerson", "");
								params.put("InstallPlace", "");
								params.put("InstallCause", "");
								params.put("InstallResult", "");
								params.put("InstallDes", "");
								params.put("TestPerson", "");
								params.put("TestTime", "");
								params.put("TestResult", "");
								params.put("TestDes", "");
								params.put("Remark", accOrInstallRem);
								params.put("OprtAccount", accOr);
								params.put("OprtTime", DateUtil.toString(DateUtil.now()));
								params.put("OprtIP", localIp);
								params.put("OprtSysI_ID", dataUnity_SysID);
								Map<String,Object> retInstalMap  = vclBaseHandler.oprtHandler(params, 2);
								if("0".equals(MapGetter.getString(retInstalMap, "ResultFlag").trim())){
									System.out.println("终端自动安装、拆除异常，详情：执行全新安装时失败！失败原因："+MapGetter.getString(retInstalMap, "Reason"));
								}else{
									
									if(DataUnity_SysCesID.equals(dataUnity_SysID)){
										System.out.println("终端自动安装终端成功，详细信息：车辆信息跟踪表ID："+msgTk_ID+"；设备ID："+vclId+"；安装的终端ID："+tmnlId);
									} else {
										String sql_Prompt ="";
										if(DataUnity_SysIemsID.equals(dataUnity_SysID)){
											List<Map<String,Object>> ParamList = mcdsDAO.execQuery(tmnlParam, new Object[]{vclId});
											if(ParamList.size()>0){
												Map<String,Object> mappp = ParamList.get(0);
												if(mappp!=null && mappp.size()>0){
													this.setNewDefaultParam(vclId,BaseNameIems,MapGetter.getString(mappp, "TmnlIL_InstallTime"));
												}
											} else {
												this.setDefaultParam(vclId,BaseNameIems);
											}
											sql_Prompt = "insert into  "+BaseNameIems+"Vcl_AutoInstallPrompt(VAIP_VclID,VAIP_Type,VAIP_SaveTime,VAIP_MsgContent)values(?,1,(select top 1 TmnlI_InstallTime from "+GlobalDBVar.getDBFullName_Conn("Tmnl_Install", "1", null)+" where TmnlI_Tmnl_ID='"+tmnlId+"' and TmnlI_Vcl_ID="+vclId+" and TmnlI_SysI_ID_I="+dataUnity_SysID+" order by TmnlI_ID desc),?)";
										}
//										if(DataUnity_SysCmmsID.equals(dataUnity_SysID)){
//											this.setDefaultParam(vclId,BaseNameCmms);
//											sql_Prompt = "insert into  "+BaseNameCmms+"Vcl_AutoInstallPrompt(VAIP_VclID,VAIP_Type,VAIP_SaveTime,VAIP_MsgContent)values(?,1,(select top 1 TmnlI_InstallTime from "+GlobalDBVar.getDBFullName_Conn("Tmnl_Install", "1", null)+" where TmnlI_Tmnl_ID='"+tmnlId+"' and TmnlI_Vcl_ID="+vclId+" and TmnlI_SysI_ID_I="+dataUnity_SysID+" order by TmnlI_ID desc),?)";
//										}
										System.out.println("终端自动安装终端成功，详细信息：车辆信息跟踪表ID："+msgTk_ID+"；设备ID："+vclId+"；安装的终端ID："+tmnlId);
										//添加详细信息至提示中心
										String MsgP_DetailMsg = "自动安装："+vehicleNumber+"设备被自动安装，终端号："+tmnlId+"，发动机号："+vclNo;
										int num = mcdsDAO.execInsert(sql_Prompt, new Object[]{vclId,MsgP_DetailMsg});
										if(num>0){
											System.out.println("跟踪信息ID："+msgTk_ID+",终端自动安装成功，并插入到提示表-----成功！");
										}else{
											System.out.println("跟踪信息ID："+msgTk_ID+",终端自动安装成功，并插入到提示表-----失败！");
										}
									}
									
								}
							}
							
						}
					}
				}
					
			}else {//设备号不存在
				//车辆跟踪信息中的终端是否被安装
				Map<String,Object> mapInstallKeyTmnl = baseDAO.execQueryOne(sqlInstallKeyTmnl, new Object[]{tmnlId});
				if(!mapInstallKeyTmnl.isEmpty()){//被安装，进行拆提醒
					//终端安装记录ID
					String tmnlInstallId = MapGetter.getString(mapInstallKeyTmnl, "TmnlIL_TmnlI_ID");
					Map<String,Object> params = new HashMap<String,Object>();
					params.put("TmnlI_ID", tmnlInstallId);
					params.put("SysI_ID_B", dataUnity_SysID);
					params.put("BackoutPerson", "");
					params.put("BackoutPlace", "");
					params.put("BackoutCause", "");
					params.put("Remark", accOrOutRem);
					params.put("OprtAccount", accOr);
					params.put("OprtTime", DateUtil.toString(DateUtil.now()));
					params.put("OprtIP", localIp);
					params.put("OprtSysI_ID", dataUnity_SysID);
					//拆除提醒，添加至提示中心
					if(StringUtil.isValid(tmnlInstallId)){
						Map<String,Object> vclMap = baseDAO.execQueryOne(sqlVclInfo, new Object[]{tmnlInstallId});
						String VehicleNumber = MapGetter.getString(vclMap, "VehicleNumber");
						String vclId = MapGetter.getString(vclMap, "TmnlI_Vcl_ID");
						String ESNNumber = MapGetter.getString(vclMap, "ESNNumber");
						String MsgP_DetailMsg = VehicleNumber+"设备,采集到的发动机号为："+vclNo+","+"系统配置发动机号为："+ESNNumber;
						//jiyongtian 20150215 iems3.7优化，修改为入库设备自动安装提示表
						if(DataUnity_SysCesID.equals(dataUnity_SysID)){
							System.out.println("跟踪信息ID："+msgTk_ID+",属于CES系统，不用插入提示！提示信息："+MsgP_DetailMsg);
						} else {
							String sql_Prompt = "";
							if(DataUnity_SysIemsID.equals(dataUnity_SysID)){
								 sql_Prompt = "insert into  "+BaseNameIems+"Vcl_AutoInstallPrompt(VAIP_VclID,VAIP_Type,VAIP_SaveTime,VAIP_MsgContent)values(?,2,GETDATE(),?)";
							}
//							if(DataUnity_SysCmmsID.equals(dataUnity_SysID)){
//								 sql_Prompt = "insert into  "+BaseNameCmms+"Vcl_AutoInstallPrompt(VAIP_VclID,VAIP_Type,VAIP_SaveTime,VAIP_MsgContent)values(?,2,GETDATE(),?)";
//							}
							int num = 0;
							if(StringUtil.isValid(VehicleNumber)){
								 num = baseDAO.execInsert(sql_Prompt, new Object[]{vclId,MsgP_DetailMsg});
							} else {
								num = -2;
							}
							if(num>0){
								System.out.println("跟踪信息ID："+msgTk_ID+",插入到提示表-----成功！");
							}else if(num == -2){
								System.out.println("跟踪信息ID："+msgTk_ID+",CMMS提示,不必插入！");
							}else{
								System.out.println("跟踪信息ID："+msgTk_ID+",插入到提示表-----失败！");
							}
						}
						
					}
					
				} else {//没有被安装
					//验证是否存在设备号为车辆跟踪信息终端ID的设备
					int vclNoEqualTmnlId = baseDAO.getCount(sqlVclExist, new Object[]{tmnlId});
					//获取此设备信息
					String sqlVe = "";
					if(DataUnity_SysIemsID.equals(dataUnity_SysID)){
						sqlVe = "select T.VCL_ID AS VehicleID,T.VCL_NO AS VehicleNumber, * from  "+GlobalDBVar.getDBFullName_Conn("vcl_info", "1", null)+" T "
								+" inner join  "+GlobalDBVar.getDBFullName_Conn("System_Vehicle", "1", null)+" ON Vcl_ID = SV_Vcl_ID AND SV_SysI_ID in ("+DataUnity_SysIemsID+","+DataUnity_SysCmmsID+") AND system_vehicle.SV_QueryPurv = 1 "
								+" left join  "+BaseNameIems+"vehicleinfo CT on T.vcl_id = CT.vehicleid where T.vcl_no=? ";
					}
//					if(DataUnity_SysCmmsID.equals(dataUnity_SysID)){
//						sqlVe = "select T.VCL_ID AS VehicleID,T.VCL_NO AS VehicleNumber, * from  "+GlobalDBVar.getDBFullName_Conn("vcl_info", "1", null)+" T "
//								+" inner join  "+GlobalDBVar.getDBFullName_Conn("System_Vehicle", "1", null)+" ON Vcl_ID = SV_Vcl_ID AND SV_SysI_ID = "+dataUnity_SysID+" AND system_vehicle.SV_QueryPurv = 1 "
//								+" inner join  "+BaseNameCmms+"vehicleinfo CT on T.vcl_id = CT.vehicleid where T.vcl_no=? ";
//					}
					if(DataUnity_SysCesID.equals(dataUnity_SysID)){
						sqlVe = "select T.VCL_ID AS VehicleID,T.VCL_NO AS VehicleNumber,CT.Vcl_EgnNo as ESNNumber, * from  "+GlobalDBVar.getDBFullName_Conn("vcl_info", "1", null)+" T "
								+" inner join  "+GlobalDBVar.getDBFullName_Conn("System_Vehicle", "1", null)+" ON Vcl_ID = SV_Vcl_ID AND SV_SysI_ID = "+dataUnity_SysID+" AND system_vehicle.SV_QueryPurv = 1 "
								+" inner join  "+BaseNameCes+"Vcl_Info CT on T.vcl_id = CT.vcl_id where T.vcl_no=? ";
					}
					if(vclNoEqualTmnlId>0){
						//存在，进行提醒
						Map<String,Object> map_Vcl = baseDAO.execQueryOne(sqlVe, new Object[]{tmnlId});
						String vehicleNumber = MapGetter.getString(map_Vcl, "VehicleNumber");
						String ESNNumber =  MapGetter.getString(map_Vcl, "ESNNumber");
						String vehicleid =  MapGetter.getString(map_Vcl, "VehicleID");
						String MsgP_DetailMsg = "终端"+tmnlId+"与设备(发动机号"+vclNo+")自动安装失败，原因是设备"+vehicleNumber+"已存在(发动机号"+ESNNumber+")";
						//jiyongtian 20150215 iems3.7优化，修改为入库设备自动安装提示表
						String sql_Prompt = "";
						if(DataUnity_SysCesID.equals(dataUnity_SysID)){
							System.out.println("跟踪信息ID："+msgTk_ID+",属于CES系统，不用插入提示！提示信息："+MsgP_DetailMsg);
						} else {
							if(DataUnity_SysIemsID.equals(dataUnity_SysID)){
								sql_Prompt = "insert into  "+BaseNameIems+" Vcl_AutoInstallPrompt(VAIP_VclID,VAIP_Type,VAIP_SaveTime,VAIP_MsgContent)values(?,2,GETDATE(),?)";
							}
							
//							if(DataUnity_SysCmmsID.equals(dataUnity_SysID)){
//								sql_Prompt = "insert into  "+BaseNameCmms+" Vcl_AutoInstallPrompt(VAIP_VclID,VAIP_Type,VAIP_SaveTime,VAIP_MsgContent)values(?,2,GETDATE(),?)";
//							}
							int num = 0;
							if(StringUtil.isValid(vehicleNumber)){
								 num = baseDAO.execInsert(sql_Prompt, new Object[]{vehicleid,MsgP_DetailMsg});
							} else {
								num = -2;
							}
							if(num>0){
								System.out.println("跟踪信息ID："+msgTk_ID+",插入到提示表-----成功！");
							}else if(num == -2){
								System.out.println("跟踪信息ID："+msgTk_ID+",CMMS提示,不必插入！");
							}else{
								System.out.println("跟踪信息ID："+msgTk_ID+",插入到提示表-----失败！");
							}
						}
						
					} else {
						//不存在，进行自动安装
						//新增终端ID为设备号的设备
						Map<String,Object> createVclMap = new HashMap<String,Object>();
						createVclMap.put("Vcl_No", tmnlId);
						createVclMap.put("Vcl_VinCode", "");
						createVclMap.put("Vcl_SysI_ID", dataUnity_SysID);
						createVclMap.put("Vcl_Remark", accOrInstallRem);
						createVclMap.put("System_Vehicle", "<System><SysI_ID>"+dataUnity_SysID+"</SysI_ID><QueryPurv>1</QueryPurv></System>");
						createVclMap.put("OprtAccount", accOr);
						createVclMap.put("OprtTime", DateUtil.toString(DateUtil.now()));
						createVclMap.put("OprtIP", localIp);
						createVclMap.put("OprtSysI_ID", dataUnity_SysID);
						Map<String,Object> retcreateValMap = vclBaseHandler.add_Vcl_Info(createVclMap);
						if("0".equals(MapGetter.getString(retcreateValMap, "ResultFlag").trim())){
							System.out.println("跟踪信息ID："+msgTk_ID+",【设备不存在,新增的设备号："+tmnlId+"】终端自动安装、拆除异常，详情：新增设备时失败！");
							continue;
						}
						String vclIdCus = MapGetter.getString(retcreateValMap, "Vcl_ID");
						boolean vclToHandlerSysFlag = this.addVclToHandlerSys(vclIdCus, tmnlId, vclNo, accOr, localIp, dataUnity_SysID,
								 DataUnity_SysIemsID, DataUnity_SysCesID, BaseNameIems, BaseNameCes);
						if(!vclToHandlerSysFlag)continue;
						
						//进行自动安装
						if(StringUtil.isNullOrEmpty(vclIdCus)&&StringUtil.isNullOrEmpty(tmnlId)){
							//执行全新的安装
							Map<String,Object> params = new HashMap<String,Object>();
							params.put("Vcl_ID", vclIdCus);
							params.put("Tmnl_ID", tmnlId);
							params.put("SysI_ID_I", dataUnity_SysID);
							params.put("InstallPerson", "");
							params.put("InstallPlace", "");
							params.put("InstallCause", "");
							params.put("InstallResult", "");
							params.put("InstallDes", "");
							params.put("TestPerson", "");
							params.put("TestTime", "");
							params.put("TestResult", "");
							params.put("TestDes", "");
							params.put("Remark", accOrInstallRem);
							params.put("OprtAccount", accOr);
							params.put("OprtTime", DateUtil.toString(DateUtil.now()));
							params.put("OprtIP", localIp);
							params.put("OprtSysI_ID", dataUnity_SysID);
							Map<String,Object> retInstalMap  = vclBaseHandler.oprtHandler(params, 2);
							if("0".equals(MapGetter.getString(retInstalMap, "ResultFlag").trim())){
								System.out.println("终端自动安装、拆除异常，详情：执行全新安装时失败！失败原因："+MapGetter.getString(retInstalMap, "Reason"));
							}else{
								
								if(DataUnity_SysCesID.equals(dataUnity_SysID)){
									System.out.println("终端自动安装终端成功，详细信息：车辆信息跟踪表ID："+msgTk_ID+"；设备ID："+vclIdCus+"；安装的终端ID："+tmnlId);
								} else {
									String sql_Prompt ="";
									if(DataUnity_SysIemsID.equals(dataUnity_SysID)){
										List<Map<String,Object>> ParamList = mcdsDAO.execQuery(tmnlParam, new Object[]{vclIdCus});
										if(ParamList.size()>0){
											Map<String,Object> mappp = ParamList.get(0);
											if(mappp!=null && mappp.size()>0){
												this.setNewDefaultParam(vclIdCus,BaseNameIems,MapGetter.getString(mappp, "TmnlIL_InstallTime"));
											}
										} else {
											this.setDefaultParam(vclIdCus,BaseNameIems);
										}
										sql_Prompt = "insert into  "+BaseNameIems+"Vcl_AutoInstallPrompt(VAIP_VclID,VAIP_Type,VAIP_SaveTime,VAIP_MsgContent)values(?,1,(select top 1 TmnlI_InstallTime from "+GlobalDBVar.getDBFullName_Conn("Tmnl_Install", "1", null)+" where TmnlI_Tmnl_ID='"+tmnlId+"' and TmnlI_Vcl_ID="+vclIdCus+" and TmnlI_SysI_ID_I="+dataUnity_SysID+" order by TmnlI_ID desc),?)";
									}
//									if(DataUnity_SysCmmsID.equals(dataUnity_SysID)){
//										this.setDefaultParam(vclIdCus,BaseNameCmms);
//										sql_Prompt = "insert into  "+BaseNameCmms+"Vcl_AutoInstallPrompt(VAIP_VclID,VAIP_Type,VAIP_SaveTime,VAIP_MsgContent)values(?,1,(select top 1 TmnlI_InstallTime from "+GlobalDBVar.getDBFullName_Conn("Tmnl_Install", "1", null)+" where TmnlI_Tmnl_ID='"+tmnlId+"' and TmnlI_Vcl_ID="+vclIdCus+" and TmnlI_SysI_ID_I="+dataUnity_SysID+" order by TmnlI_ID desc),?)";
//									}
									System.out.println("终端自动安装终端成功，详细信息：车辆信息跟踪表ID："+msgTk_ID+"；设备ID："+vclIdCus+"；安装的终端ID："+tmnlId);
									//获取此设备信息
									if(DataUnity_SysIemsID.equals(dataUnity_SysID)){
										sqlVe = "select T.VCL_ID AS VehicleID,T.VCL_NO AS VehicleNumber, * from  "+GlobalDBVar.getDBFullName_Conn("vcl_info", "1", null)+" T "
												+" inner join  "+GlobalDBVar.getDBFullName_Conn("System_Vehicle", "1", null)+" ON Vcl_ID = SV_Vcl_ID AND SV_SysI_ID in ("+DataUnity_SysIemsID+","+DataUnity_SysCmmsID+") AND system_vehicle.SV_QueryPurv = 1 "
												+" inner join  "+BaseNameIems+"vehicleinfo CT on T.vcl_id = CT.vehicleid where T.vcl_id=? ";
									}
//									if(DataUnity_SysCmmsID.equals(dataUnity_SysID)){
//										sqlVe = "select T.VCL_ID AS VehicleID,T.VCL_NO AS VehicleNumber, * from  "+GlobalDBVar.getDBFullName_Conn("vcl_info", "1", null)+" T "
//												+" inner join  "+GlobalDBVar.getDBFullName_Conn("System_Vehicle", "1", null)+" ON Vcl_ID = SV_Vcl_ID AND SV_SysI_ID = "+dataUnity_SysID+" AND system_vehicle.SV_QueryPurv = 1 "
//												+" inner join  "+BaseNameCmms+"vehicleinfo CT on T.vcl_id = CT.vehicleid where T.vcl_id=? ";
//									}
									if(DataUnity_SysCesID.equals(dataUnity_SysID)){
										sqlVe = "select T.VCL_ID AS VehicleID,T.VCL_NO AS VehicleNumber,CT.Vcl_EgnNo as ESNNumber, * from  "+GlobalDBVar.getDBFullName_Conn("vcl_info", "1", null)+" T "
												+" inner join  "+GlobalDBVar.getDBFullName_Conn("System_Vehicle", "1", null)+" ON Vcl_ID = SV_Vcl_ID AND SV_SysI_ID = "+dataUnity_SysID+" AND system_vehicle.SV_QueryPurv = 1 "
												+" inner join  "+BaseNameCes+"Vcl_Info CT on T.vcl_id = CT.vcl_id where T.vcl_id=? ";
									}
									Map<String,Object> map_Vcl = baseDAO.execQueryOne(sqlVe, new Object[]{vclIdCus});
									String VehicleNumber = MapGetter.getString(map_Vcl, "VehicleNumber");
									String ESNNumber = MapGetter.getString(map_Vcl, "ESNNumber");
									//添加详细信息至提示中心
									String MsgP_DetailMsg = "自动安装："+VehicleNumber+"设备被自动安装，终端号："+tmnlId+"，发动机号："+ESNNumber;
									int num = mcdsDAO.execInsert(sql_Prompt, new Object[]{vclIdCus,MsgP_DetailMsg});
									if(num>0){
										System.out.println("跟踪信息ID："+msgTk_ID+",终端自动安装成功，并插入到提示表-----成功！");
									}else{
										System.out.println("跟踪信息ID："+msgTk_ID+",终端自动安装成功，并插入到提示表-----失败！");
									}
								}
								
							}
						}
					}
				}
			}
		 }else{
			//无效终端，不进行任何操作，结束
			System.out.println("跟踪信息ID："+msgTk_ID+",无效终端或终端不存在或终端不数据系统IEMS、CMMS、CES！");
		}
			
		 }else {
			 //发动机号不符合要求
			 System.out.println("跟踪信息ID："+msgTk_ID+",发动机号不符合要求！");
		 }	
	}
		System.out.println("自动安装作业结束：["+DateUtil.toString(DateUtil.now(), "yyyy-MM-dd HH:mm:ss")+"~");
}
	
	
	/**
	 * 增加设备到业务系统
	 * @return
	 */
	public boolean addVclToHandlerSys(String vclId,String vclNo,String ESNNumber,String accOr,String oprtIp,String sysID,
			String DataUnity_SysIemsID,String DataUnity_SysCesID,String BaseNameIems,String BaseNameCes){
		boolean flag = false;
		if(StringUtil.isNullOrEmpty(vclId)&&StringUtil.isNullOrEmpty(vclNo)
				&&StringUtil.isNullOrEmpty(ESNNumber)){
			List<String> sqlList = new ArrayList<String>();
			List<Object[]> paramList = new ArrayList<Object[]>();
			String addVclSQL = "";
			String addVclLogSQL = "";
			if(DataUnity_SysIemsID.equals(sysID)){
				addVclSQL = "insert into "+BaseNameIems+"VehicleInfo(VehicleID,VehicleKindID,VehicleNumber,VehicleTypeID,ESNNumber,Province,VehicleBuildTime,UsefulLife)" +
						"values(?,?,?,?,?,?,GETDATE(),'2099-12-31');";
				sqlList.add(addVclSQL);
				paramList.add(new Object[]{vclId,AutoInstallVclService.vclKindId,vclNo,AutoInstallVclService.vclTypeId,ESNNumber,AutoInstallVclService.provinceId});
				addVclLogSQL = "insert into "+BaseNameIems+"VehicleInfo_OperateLog(OperateAccount,OperateTime,OperateIP,OperateTypeID,VehicleID,VehicleKindID," +
						"VehicleNumber,VehicleTypeID,ESNNumber,Province,VehicleBuildTime,UsefulLife)values(?,GETDATE(),?,1,?,?,?,?,?,?,GETDATE(),'2099-12-31');";
				sqlList.add(addVclLogSQL);
				paramList.add(new Object[]{accOr,oprtIp,vclId,AutoInstallVclService.vclKindId,vclNo,AutoInstallVclService.vclTypeId,ESNNumber,AutoInstallVclService.provinceId});
			}
//			if(DataUnity_SysCmmsID.equals(sysID)){
//				addVclSQL = "insert into "+BaseNameCmms+"VehicleInfo(VehicleID,VehicleKindID,VehicleNumber,VehicleTypeID,ESNNumber,Province,VehicleBuildTime,UsefulLife)" +
//						"values(?,?,?,?,?,?,GETDATE(),'2099-12-31');";
//				sqlList.add(addVclSQL);
//				paramList.add(new Object[]{vclId,AutoInstallVclService.vclKindId,vclNo,AutoInstallVclService.vclTypeId,ESNNumber,AutoInstallVclService.provinceId});
//				addVclLogSQL = "insert into "+BaseNameCmms+"VehicleInfo_OperateLog(OperateAccount,OperateTime,OperateIP,OperateTypeID,VehicleID,VehicleKindID," +
//						"VehicleNumber,VehicleTypeID,ESNNumber,Province,VehicleBuildTime,UsefulLife)values(?,GETDATE(),?,1,?,?,?,?,?,?,GETDATE(),'2099-12-31');";
//				sqlList.add(addVclLogSQL);
//				paramList.add(new Object[]{accOr,oprtIp,vclId,AutoInstallVclService.vclKindId,vclNo,AutoInstallVclService.vclTypeId,ESNNumber,AutoInstallVclService.provinceId});
//			}
			if(DataUnity_SysCesID.equals(sysID)){
				addVclSQL = "insert into "+BaseNameCes+"vcl_info(Vcl_ID,Vcl_No,Vcl_EgnNo,Vcl_Eff,Vcl_Exp)" +
						"values(?,?,?,GETDATE(),'2099-12-31');";
				sqlList.add(addVclSQL);
				paramList.add(new Object[]{vclId,vclNo,ESNNumber});
				addVclLogSQL = "insert into "+BaseNameCes+"Vcl_Info_OperateLog(VclOL_OprtAccount,VclOL_OprtTime,VclOL_OprtIP,VclOL_SysOT_ID,VclOL_Vcl_ID,VclOL_Vcl_No," +
						"VclOL_VCL_EgnNo,VclOL_Vcl_Eff,VclOL_Vcl_Exp)values(?,GETDATE(),?,1,?,?,?,GETDATE(),'2099-12-31');";
				sqlList.add(addVclLogSQL);
				paramList.add(new Object[]{accOr,oprtIp,vclId,vclNo,ESNNumber});
			}
			flag = mcdstDAO.batch(sqlList, paramList);
		}
		
		return flag;
	}
	
	/**
	 * 设置新一批默认终端参数()
	 * @param vclId
	 * @param dataBaseName
	 */
	public void setNewDefaultParam(String vclId,String dataBaseName,String tmnlInstallTime){
		List<String> sqlList = new ArrayList<String>();
		List<Object[]> paramList = new ArrayList<Object[]>();
		//UDS
		String expHisSQL_UDS = "update "+dataBaseName+"Vcl_Sys_HisParam set VSHP_Exp=GETDATE() " +
				"where VSHP_SetType=1 and VSHP_Exp>=GETDATE() and VSHP_SetObj_ID=?";
		sqlList.add(expHisSQL_UDS);
		paramList.add(new Object[]{vclId});
		
		String initUdsSql = "insert into "+dataBaseName+"Vcl_Sys_HisParam(VSHP_SysU_ID,VSHP_SetObj_ID,VSHP_SetType,VSHP_Eff,VSHP_Exp)"+	
				" select TmnlUPBP_ParamID,?,1,TmnlIL_InstallTime,'2099-12-31' from "+GlobalDBVar.getDBFullName_Conn("tmnl_install_last", "1", null)+" " +
				" inner join  "+GlobalDBVar.getDBFullName_Conn("tmnl_info", "1", null)+" on TmnlIL_Tmnl_ID = Tmnl_ID " +
				" inner join  "+GlobalDBVar.getDBFullName_Conn("Tmnl_Info_UdsPgnBatch_Param", "1", null)+" on TmnlUPBP_TmnlUPB_ID = Tmnl_TmnlUPB_ID " +
				" where TmnlIL_vcl_id = ? and TmnlUPBP_ParamType = 1 ";
		sqlList.add(initUdsSql);
		paramList.add(new Object[]{vclId,vclId});
		
		
		//PGN
		String expHisSQL_PGN = "update Vcl_ParamSetPGN set VclPSP_Exp=GETDATE() " +
				"where VclPSP_SetType=1 and VclPSP_Exp>=GETDATE() and VclPSP_SetObj_ID=?";
		sqlList.add(expHisSQL_PGN);
		paramList.add(new Object[]{vclId});
		
		String initPgnSql = "insert into "+dataBaseName+"Vcl_ParamSetPGN(VclPSP_SysPIP_ID,VclPSP_SetObj_ID,VclPSP_SetType,VclPSP_Eff,VclPSP_Exp)"+	
				" select TmnlUPBP_ParamID,?,1,TmnlIL_InstallTime,'2099-12-31' from "+GlobalDBVar.getDBFullName_Conn("tmnl_install_last", "1", null)+" " +
				" inner join  "+GlobalDBVar.getDBFullName_Conn("tmnl_info", "1", null)+" on TmnlIL_Tmnl_ID = Tmnl_ID " +
				" inner join  "+GlobalDBVar.getDBFullName_Conn("Tmnl_Info_UdsPgnBatch_Param", "1", null)+" on TmnlUPBP_TmnlUPB_ID = Tmnl_TmnlUPB_ID " +
				" where TmnlIL_vcl_id = ? and TmnlUPBP_ParamType = 2 ";
		sqlList.add(initPgnSql);
		paramList.add(new Object[]{vclId,vclId});
		
		//频率
		String freqTmnl = "insert into "+GlobalDBVar.getDBFullName_Conn("Msg_Tmnl_Set", "3", null)+"(MsgTlS_Vcl_ID,MsgTlS_SetTime,MsgTlS_TimeOver,MsgTlS_SysCT_ID," +
				"MsgTlS_SysCS_ID,MsgTlS_UDSCyc,MsgTlS_PGNCyc)" +
				"values(?,?,0,1,1,"+FREQUDSDEF*6+","+FREQPGNDEF*6+");";
		sqlList.add(freqTmnl);
		paramList.add(new Object[]{vclId,tmnlInstallTime});
		
		String expFre = "update "+dataBaseName+"VclParaFreqInfo set VclPFI_Exp=? where VclPFI_SetType=1 and VclPFI_SetObjID=? and VclPFI_Exp>=GETDATE()";
		sqlList.add(expFre);
		paramList.add(new Object[]{tmnlInstallTime,vclId});
		
		String addFre = "insert into "+dataBaseName+"VclParaFreqInfo(VclPFI_SetType,VclPFI_SetObjID,VclPFI_SetFreqUDS,VclPFI_ValidDateUDS," +
				"VclPFI_ChkStatusUDS,VclPFI_SetFreqPGN,VclPFI_ValidDatePGN,VclPFI_ChkStatusPGN,VclPFI_Eff,VclPFI_Exp)" +
				"values(1,?,?,?,0,?,?,0,GETDATE(),?);";
		sqlList.add(addFre);
		paramList.add(new Object[]{vclId,1,"2099-12-31",1,"2099-12-31","2099-12-31"});
		String sqlUdsPgnId = "";
		if(this.validVclConfig(vclId)){
			 sqlUdsPgnId ="update VclConfig set vclconfig_msgtls_id_uds = (select IDENT_CURRENT('"+GlobalDBVar.getDBFullName_Conn("Msg_Tmnl_Set","3",null)+"') ),vclconfig_msgtls_id_pgn = (select IDENT_CURRENT('"+GlobalDBVar.getDBFullName_Conn("Msg_Tmnl_Set","3",null)+"') ) " +
					" where VclConfig_Vcl_ID = "+vclId+" "; 
		} else {
			 sqlUdsPgnId ="insert into  VclConfig (VclConfig_Vcl_ID,vclconfig_msgtls_id_uds,vclconfig_msgtls_id_pgn) values ("+vclId+",IDENT_CURRENT('"+GlobalDBVar.getDBFullName_Conn("Msg_Tmnl_Set","3",null)+"'),IDENT_CURRENT('"+GlobalDBVar.getDBFullName_Conn("Msg_Tmnl_Set","3",null)+"')) ";
		}
		sqlList.add(sqlUdsPgnId);
		paramList.add(new Object[]{});
		//执行批处理
		tranDAO.batch(sqlList, paramList);
		
	}
	
	/**
	 * 设置默认参数
	 * @param vclId
	 * @return
	 */
	public void setDefaultParam(String vclId,String dataBaseName){
		List<String> sqlList = new ArrayList<String>();
		List<Object[]> paramList = new ArrayList<Object[]>();
		//UDS
		String expHisSQL_UDS = "update "+dataBaseName+"Vcl_Sys_HisParam set VSHP_Exp=GETDATE() " +
				"where VSHP_SetType=1 and VSHP_Exp>=GETDATE() and VSHP_SetObj_ID=?";
		sqlList.add(expHisSQL_UDS);
		paramList.add(new Object[]{vclId});
		
		String istSQL_UDS = "insert into "+dataBaseName+"Vcl_Sys_HisParam(VSHP_SysU_ID,VSHP_SetObj_ID,VSHP_SetType,VSHP_Eff,VSHP_Exp)" +
			"select Sys_HP_SysU_ID,?,1,GETDATE(),'2099-12-31' from "+dataBaseName+"Sys_HisParam where Sys_HP_IsPub=0";
		sqlList.add(istSQL_UDS);
		paramList.add(new Object[]{vclId});
		
		String expTmnlSQL_UDS = "update "+GlobalDBVar.getDBFullName_Conn("Msg_UDS_Set", "3", null)+" set MsgUS_SysCS_ID=3 where MsgUS_Vcl_ID=? and MsgUS_SysCS_ID=1";
		sqlList.add(expTmnlSQL_UDS);
		paramList.add(new Object[]{vclId});
		
		String sendTmnlSQL_UDS = "insert into "+GlobalDBVar.getDBFullName_Conn("Msg_UDS_Set", "3", null)+"(MsgUS_Vcl_ID,MsgUS_SetTime,MsgUS_TimeOver,MsgUS_SysCT_ID,MsgUS_SysCS_ID,MsgUS_UDSCount)" +
				"values(?,GETDATE(),0,1,1,(select count(1) from "+dataBaseName+"Sys_HisParam where Sys_HP_IsPub=0));";
		sqlList.add(sendTmnlSQL_UDS);
		paramList.add(new Object[]{vclId});
		
		String sendTmnlCSQL_UDS = "insert into "+GlobalDBVar.getDBFullName_Conn("Msg_UDS_Set_Content", "3", null)+" (MsgUSC_MsgUS_ID,MsgUSC_UDSID)" +
				"select IDENT_CURRENT('"+GlobalDBVar.getDBFullName_Conn("Msg_UDS_Set", "3", null)+"'),SysU_UDSID from "+dataBaseName+"Sys_HisParam " +
				"left join "+GlobalDBVar.getDBFullName_Conn("Sys_UDS", "1", null)+" on SysU_ID=Sys_HP_SysU_ID " +
				"where Sys_HP_IsPub=0 and SysU_UDSID!='' and SysU_UDSID is not null;";
		sqlList.add(sendTmnlCSQL_UDS);
		paramList.add(new Object[]{});
		
		//PGN
		String expHisSQL_PGN = "update Vcl_ParamSetPGN set VclPSP_Exp=GETDATE() " +
				"where VclPSP_SetType=1 and VclPSP_Exp>=GETDATE() and VclPSP_SetObj_ID=?";
		sqlList.add(expHisSQL_PGN);
		paramList.add(new Object[]{vclId});
		
		String istSQL_PGN = "insert into "+dataBaseName+"Vcl_ParamSetPGN (VclPSP_SysPIP_ID,VclPSP_SetObj_ID,VclPSP_SetType,VclPSP_Eff,VclPSP_Exp)" +
				"select SysHPP_SysPIP_ID,?,1,GETDATE(),'2099-12-31' from "+dataBaseName+"Sys_HisParam_PGN where SysHPP_IsPub=0";
		sqlList.add(istSQL_PGN);
		paramList.add(new Object[]{vclId});
		
		String expTmnlSQL_PGN = "update "+GlobalDBVar.getDBFullName_Conn("Msg_PGN_Set", "3", null)+" set MsgPS_SysCS_ID=3 where MsgPS_Vcl_ID=? and MsgPS_SysCS_ID=1";
		sqlList.add(expTmnlSQL_PGN);
		paramList.add(new Object[]{vclId});
		
		String sendTmnlSQL_PGN = "insert into "+GlobalDBVar.getDBFullName_Conn("Msg_PGN_Set", "3", null)+"(MsgPS_Vcl_ID,MsgPS_SetTime,MsgPS_TimeOver,MsgPS_SysCT_ID,MsgPS_SysCS_ID,MsgPS_PGNCount)" +
				"values(?,GETDATE(),0,1,1,(select COUNT(1) from (select SysPIP_SysPGN_ID from "+dataBaseName+"Sys_HisParam_PGN " +
				"inner join "+GlobalDBVar.getDBFullName_Conn("Sys_PGN_ID_Para", "1", null)+" on SysHPP_SysPIP_ID=SysPIP_ID where SysHPP_IsPub=0 group by SysPIP_SysPGN_ID)tmp));";
		sqlList.add(sendTmnlSQL_PGN);
		paramList.add(new Object[]{vclId});
		
		String sendTmnlCSQL_PGN = "insert into "+GlobalDBVar.getDBFullName_Conn("Msg_PGN_Set_Content", "3", null)+" (MsgPSC_MsgPS_ID,MsgPSC_SysPI_ID,MsgPSC_SysPI_PGNCode,MsgPSC_IsApply,MsgPSC_PackageCount)" +
				"select IDENT_CURRENT('"+GlobalDBVar.getDBFullName_Conn("Msg_PGN_Set", "3", null)+"'),SysPI_ID,SysPI_PGNCode,SysPI_IsApply,SysPI_BagNumber " +
				"from "+GlobalDBVar.getDBFullName_Conn("Sys_PGN_ID", "1", null)+" where SysPI_ID in(" +
				"select SysPIP_SysPGN_ID from "+dataBaseName+"Sys_HisParam_PGN " +
				"inner join "+GlobalDBVar.getDBFullName_Conn("Sys_PGN_ID_Para", "1", null)+" on SysHPP_SysPIP_ID=SysPIP_ID " +
				"where SysHPP_IsPub=0 " +
				"group by SysPIP_SysPGN_ID)";
		sqlList.add(sendTmnlCSQL_PGN);
		paramList.add(new Object[]{});
		
		//频率
		String freqTmnl = "insert into "+GlobalDBVar.getDBFullName_Conn("Msg_Tmnl_Set", "3", null)+"(MsgTlS_Vcl_ID,MsgTlS_SetTime,MsgTlS_TimeOver,MsgTlS_SysCT_ID," +
				"MsgTlS_SysCS_ID,MsgTlS_UDSCyc,MsgTlS_PGNCyc)" +
				"values(?,GETDATE(),0,1,1,"+FREQUDSDEF*60+","+FREQPGNDEF*60+");";
		sqlList.add(freqTmnl);
		paramList.add(new Object[]{vclId});
		
		String expFre = "update "+dataBaseName+"VclParaFreqInfo set VclPFI_Exp=GETDATE() where VclPFI_SetType=1 and VclPFI_SetObjID=? and VclPFI_Exp>=GETDATE()";
		sqlList.add(expFre);
		paramList.add(new Object[]{vclId});
		
		String addFre = "insert into "+dataBaseName+"VclParaFreqInfo(VclPFI_SetType,VclPFI_SetObjID,VclPFI_SetFreqUDS,VclPFI_ValidDateUDS," +
				"VclPFI_ChkStatusUDS,VclPFI_SetFreqPGN,VclPFI_ValidDatePGN,VclPFI_ChkStatusPGN,VclPFI_Eff,VclPFI_Exp)" +
				"values(1,?,?,?,0,?,?,0,GETDATE(),?);";
		sqlList.add(addFre);
		paramList.add(new Object[]{vclId,FREQUDSDEF,"2099-12-31",FREQPGNDEF,"2099-12-31","2099-12-31"});
		
		//设备与配置关系表
		String paramConfig = "insert into CTY_MS.DBO.VclConfig" +
				"(VclConfig_Vcl_ID,VclConfig_TmnlTestResult,VclConfig_MsgUS_ID," +
				"VclConfig_MsgPS_ID,VclConfig_MsgTlS_ID_PGN,VclConfig_MsgTlS_ID_UDS," +
				"VclConfig_MsgTlSDB_ID,VclConfig_VclLI_ID) values (?,null," +
				" IDENT_CURRENT('"+GlobalDBVar.getDBFullName_Conn("Msg_UDS_Set", "3", null)+"')," +
				" IDENT_CURRENT('"+GlobalDBVar.getDBFullName_Conn("Msg_PGN_Set", "3", null)+"')," +
				" IDENT_CURRENT('"+GlobalDBVar.getDBFullName_Conn("Msg_Tmnl_Set", "3", null)+"')," +
				" IDENT_CURRENT('"+GlobalDBVar.getDBFullName_Conn("Msg_Tmnl_Set", "3", null)+"'),null,null)";
		sqlList.add(paramConfig);
		paramList.add(new Object[]{vclId});		
		//执行批处理
		mcdstDAO.batch(sqlList, paramList);
	}
	
	/**
	 * 验证设备在设备配置关系表是否进行了设置是否进行了设置
	 * 
	 * @param vclId
	 * @return true：已设置 false：未设置
	 */
	public boolean validVclConfig(String vclId) {
		String strSQL = "select COUNT(1) cnt from (select * from VclConfig "
				+ " )a where VclConfig_Vcl_ID=? ";
		int cnt = baseDAO.getCount(strSQL,
				new Object[] { vclId });
		if (cnt > 0) {
			return true;
		} else {
			return false;
		}
	}
	
	public static void main(String[] args) {
		Map<String,Object> params = new HashMap<String,Object>();
		params.put("Vcl_ID", "213d");
		params.put("Tmnl_ID", "435");
	}
}
